from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time
from random import randint
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pymongo


class WphShop:
    mongo_client = pymongo.MongoClient('localhost', 27017)
    collection = mongo_client['wph']['sp_info']

    driver_path = Service(executable_path='./chromedriver.exe')
    options = Options()
    options.add_experimental_option("detach", True)
    options.add_argument('--start-maximized')
    options.add_argument('blink-settings=imagesEnabled=false')
    options.add_experimental_option('excludeSwitches', ['enable-automation'])
    browser = webdriver.Chrome(service=driver_path, options=options)

    @classmethod
    def get_index(cls):
        cls.browser.get('https://www.vip.com/')
        wait = WebDriverWait(cls.browser, 10)
        ipt = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="J-search"]/div[1]/input')))
        ipt.send_keys('手机')
        btn = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="J-search"]/div[1]/a/span')))
        time.sleep(2)
        btn.click()
        time.sleep(randint(1, 3))

    @classmethod
    def drop_down(cls):
        for i in range(1, 12):
            js = f"document.documentElement.scrollTop={i}*1000"
            cls.browser.execute_script(js)
            time.sleep(randint(1, 3))

    @classmethod
    def parse_data(cls):
        cls.drop_down()

        div_lis = cls.browser.find_elements(
            By.XPATH,
            '//*[@id="J_searchCatList"]/div[@class="c-goods-item  J-goods-item c-goods-item--auto-width"]'

        )
        for div in div_lis:
            price = div.find_element(By.XPATH,
                                     './/div[@class="c-goods-item__sale-price J-goods-item__sale-price"]').text

            title = div.find_element(By.XPATH, './/div[2]/div[2]').text
            item = {
                'title': title,
                'price': price
            }
            print(item)
            cls.save_data(item)
        time.sleep(randint(1, 4))
        cls.next_page()

    @classmethod
    def save_data(cls, item):
        cls.collection.insert_one(item)

    @classmethod
    def next_page(cls):
        try:
            next_btn = cls.browser.find_element(By.XPATH,'//*[@id="J_nextPage_link"]/i')
            if next_btn:
                next_btn.click()
                cls.parse_data()
            else:
                cls.browser.close()
        except Exception as e:
            print(e)
            cls.browser.quit()

    @classmethod
    def run(cls):
        cls.get_index()
        cls.parse_data()



if __name__ == '__main__':
    WphShop().run()
